<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script>
    /* 
      解构赋值，是一种快捷的赋值方式。
      数据的解构复制，只需要将变量和数据结构写一致，那么就可以获取到数据对应位置的值
    
      数组的解构赋值
        var [a, b, c] = [1,2,3]
          abc就是123  

        const [,,[a1, a2],b,c] = [1,2,[3,4],5,6]
        console.log(a1, a2, b, c)
    
      对象解构赋值
        const {name, age} = {name: '张三', age: 18}

        因为对象没有顺序，所以name和age默认情况下必须和对象中的属性名一致

        如果想要使用别名
        const {name: n, age: a} = {name: '张三', age: 18}

        
      结构赋值允许默认值
        如果解构赋值没有得到值，会使用默认值
        const [a=1] = []
          a 为1 
        const [a=1] = [2]
          a 为2

      函数的参数的解构
        将参数直接解构，获取内部的属性作为变量名，就不需要在参数中再次获取

        原来的写法
        function fn (obj) {
          var name = obj.name
          var age = obj.age
        }

        解构写法1 
        function fn (obj) {
          var {name, age} = obj
        }

        解构写法2
        function fn ({name, age}) {
          // 直接得到name和age
        }

     */
    var arr = [1,2,3,4,5]

    /* var a = 1,
      b = 2,
      c = 3, 
      d = 4,
      e = 5 */
  

    /* const [a, b, c, d, e] = arr
    console.log(a, b, c, d, e) */

    const obj = {
      name: '张三',
      age: 18
    }

    /* var name = obj.name,
      age = obj.age */

    const {name: n, age: a} = obj

    console.log(n, a)

    function fn ({name, age}) {
      
      console.log(name, age)
    }

    /* function fn (obj) {
      // var name = obj.name
      // var age = obj.age
      const {name, age} = obj
      console.log(name, age)
    } */


    fn ({name: '张三', age: 18})



    var [e,f,g]  = [1,2,3]
    console.log(e,f,g)
   const [t,[k,l],m] = [3,[4,5],6]
   console.log(t,k,l,m)

    var arr = [1,2,3,4]
    const [z,x,c,v] = arr
    console.log(z,x,c,v)

    const {name,age} = {name:'nana',age:24}
    console.log(name,age)

    function fn({name,age}){
      console.log(name,age)
    }
    fn({name:'nana',age:'23'})
  </script>
</body>
</html>